/**
 * Created by superxing-cto on 17/1/16.
 */
var Record = Record || (function () {
        var self = null;
        var localId = null;
        var is_voice = false;
        var recordTimer = null;
        var myScroll2 = null;
        var record_type = 'system';
        var area1 = document.getElementById("scrollBox1");
        var ul3 = document.getElementById("ul3");
        var ul4 = document.getElementById("ul4");
        ul4.innerHTML = ul3.innerHTML;
        ;

        function Record() {
            self = this;
            this.init();
        }

        Record.prototype = {
            init: function () {
                this.addEvent();
            },
            addEvent: function () {
                $(document).on('click', '*[record-action]', function (e) {
                    var target = $(this),
                        action = $(this).attr('record-action');
                    self[action](target);
                });
                // wx.ready(function () {
                $(document).on('touchstart', '*[record-touch-action]', function (e) {
                    var target = $(this);
                    e.preventDefault();
                    self.start_record(target);
                }).on('touchmove', '*[record-touch-action]', function (e) {
                    e.preventDefault();
                    hasMoved = true;
                    y = event.targetTouches[0].pageY;
                    z += x - y;
                    $(document).scrollTop(z);
                }).on('touchend', '*[record-touch-action]', function (e) {
                    var target = $(this);
                    self.end_record(target);
                });
                // });
            },
            start_record: function (target) {
                if (!target.hasClass('recorded')) {
                    //计数
                    recordTimer = setTimeout(function () {
                        start_interval();
                        $('.rpulse').eq(2).addClass('rpulse2');
                        $('.rpulse').eq(3).addClass('rpulse3');
                        wx.startRecord({
                            success: function () {
                                timeBlock();
                                is_voice = true;
                                localStorage.rainAllowRecord = 'true';
                            },
                            cancel: function () {
                                alert('用户拒绝授权录音');
                            }
                        });
                    }, 300);
                }
            },
            end_record: function (target) {
                if (!target.hasClass('recorded')) {
                    clearTimeout(recordTimer);
                    clearInterval(myScroll2);
                    area1.scrollTop = 0;
                    $('.rpulse').eq(2).removeClass('rpulse2');
                    $('.rpulse').eq(3).removeClass('rpulse3');
                    wx.stopRecord({
                        success: function (res) {
                            localId = res.localId;
                            target.find('img').attr('src', '/assets/declaimer/img/icon_play.svg');
                            target.addClass('recorded');
                            data_count(2, 'record');
                        },
                        fail: function (res) {
                            // alert(JSON.stringify(res));
                        }
                    });
                } else {
                    this.play_record(target);
                }
            },
            play_record: function (target) {
                if (!target.hasClass('played')) {
                    target.addClass('played');
                    $('.rpulse').eq(2).addClass('rpulse2');
                    $('.rpulse').eq(3).addClass('rpulse3');
                    target.find('img').attr('src', '/assets/declaimer/img/icon_suspend.svg');
                    wx.playVoice({
                        localId: localId,
                        success: function (res) {
                            start_interval();
                        }
                    });
                    wx.onVoicePlayEnd({
                        success: function (res) {
                            localId = res.localId; // 返回音频的本地ID
                            clearInterval(myScroll2);
                            area1.scrollTop = 0;
                            $('.rpulse').eq(2).removeClass('rpulse2');
                            $('.rpulse').eq(3).removeClass('rpulse3');
                            target.removeClass('played');
                            target.find('img').attr('src', '/assets/declaimer/img/icon_play.svg');
                        }
                    });
                } else {
                    target.removeClass('played')
                    $('.rpulse').eq(2).removeClass('rpulse2');
                    $('.rpulse').eq(3).removeClass('rpulse3');
                    target.find('img').attr('src', '/assets/declaimer/img/icon_play.svg');
                    this.pause_record(target);
                }
            },
            pause_record: function (target) {
                if (localId != null) {
                    self.stop_record();
                    clearInterval(myScroll2);
                    area1.scrollTop = 0;
                }
            },
            stop_record: function () {
                if (localId != null) {
                    wx.stopVoice({
                        localId: localId // 需要停止的音频的本地ID，由stopRecord接口获得
                    });
                }
            },
            reload_record: function (target) {
                area1.scrollTop = 0;
                $('#player1').find('img').attr('src', '/assets/declaimer/img/icon_speak.svg');
                $('#player1').removeClass('recorded').removeClass('played');
                $('.rpulse').eq(2).removeClass('rpulse2');
                $('.rpulse').eq(3).removeClass('rpulse3');
                self.pause_record();
                localId = null;
            },
            save_record: function (target) {
                if (!target.hasClass('disabled')) {
                    if ($('input[name="record_type"]').val() == 'system') {
                        $('form').submit();
                    }
                    if ($('input[name="record_type"]').val() == 'own') {
                        var callback = function (serverId) {
                            target.addClass('disabled');
                            target.text('提交中....');
                            $('input[name="server_id"]').val(serverId);
                            $('form').submit();
                        };
                        self.upload_record(callback);
                    }
                }
            },
            upload_record: function (callback) {
                if (localId == null) {
                    alert('您还未进行录制!');
                    return false;
                }
                wx.uploadVoice({
                    localId: localId,
                    isShowProgressTips: 1,
                    success: function (res) {
                        var serverId = res.serverId;
                        callback.call(this, serverId);
                    },
                    error: function (res) {
                        alert('录音上传失败');
                    }
                });
            },
            check_box: function (target) {
                if (target.find('.notcheck').length > 0) {
                    $('.check').removeClass('check').addClass('notcheck');
                    target.find('.notcheck').addClass("check").removeClass("notcheck");
                    if (target.data('type') == 'system') {
                        $('div[node-type="system"]').css('display', '');
                        $('div[node-type="own"]').css('display', 'none');
                        $('.rpulse').eq(2).removeClass('rpulse2');
                        $('.rpulse').eq(3).removeClass('rpulse3');
                        if ($('#player1').hasClass('played')) {
                            $('#player1').find('img').attr('src', '/assets/declaimer/img/icon_play.svg');
                            $('#player1').removeClass('played');
                        }
                        $('input[name="record_type"]').val('system');
                        record_type = 'system';
                        self.pause_record($('#player1'))
                    }
                    if (target.data('type') == 'own') {
                        $('#player').hasClass('is_played') ? $('#player').trigger('click') : '';
                        $('div[node-type="system"]').css('display', 'none');
                        $('div[node-type="own"]').css('display', '');
                        $('input[name="record_type"]').val('own');
                        record_type = 'own';
                    }
                }
            }
        }
        function start_interval() {
            myScroll2 = setInterval(function () {
                if (area1.scrollTop > ul3.offsetHeight) {
                    area1.scrollTop = 0;
                } else {
                    area1.scrollTop++;
                }
            }, 100);
        }

        new Record();
    })(window.jQuery)